All files / models db-interface.ts

100% Statements 35/35
100% Branches 4/4
100% Functions 11/11
100% Lines 33/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                  14x   14x   14x                       320x 320x 320x 320x 320x               22x 22x 12x     10x 10x 10x 1x   10x 9x   10x 10x 10x 10x     1x 1x         10x             58x 58x 58x 8x 8x 8x                   14x 1x   14x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
 
import { ErrorFactory } from '@firebase/util';
 
import Errors from './errors';
 
export default class DBInterface {
  private DB_NAME_: string;
  private dbVersion_: number;
  private openDbPromise_: Promise<IDBDatabase>;
  protected errorFactory_: ErrorFactory<string>;
  protected TRANSACTION_READ_WRITE: IDBTransactionMode;
 
  /**
   * @param {string} dbName
   * @param {number} dbVersion
   */
  constructor(dbName, dbVersion) {
    this.errorFactory_ = new ErrorFactory('messaging', 'Messaging', Errors.map);
    this.DB_NAME_ = dbName;
    this.dbVersion_ = dbVersion;
    this.openDbPromise_ = null;
    this.TRANSACTION_READ_WRITE = 'readwrite';
  }
 
  /**
   * Get the indexedDB as a promsie.
   * @protected
   * @return {!Promise<!IDBDatabase>} The IndexedDB database
   */
  openDatabase() {
    if (this.openDbPromise_) {
      return this.openDbPromise_;
    }
 
    this.openDbPromise_ = new Promise((resolve, reject) => {
      const request = indexedDB.open(this.DB_NAME_, this.dbVersion_);
      request.onerror = event => {
        reject((<IDBRequest>event.target).error);
      };
      request.onsuccess = event => {
        resolve((<IDBRequest>event.target).result);
      };
      request.onupgradeneeded = event => {
        try {
          var db = (<IDBRequest>event.target).result;
          this.onDBUpgrade(db);
        } catch (err) {
          // close the database as it can't be used.
          db.close();
          reject(err);
        }
      };
    });
 
    return this.openDbPromise_;
  }
 
  /**
   * Close the currently open database.
   * @return {!Promise} Returns the result of the promise chain.
   */
  closeDatabase() {
    return Promise.resolve().then(() => {
      if (this.openDbPromise_) {
        return this.openDbPromise_.then(db => {
          db.close();
          this.openDbPromise_ = null;
        });
      }
    });
  }
 
  /**
   * @protected
   * @param {!IDBDatabase} db
   */
  onDBUpgrade(db) {
    throw this.errorFactory_.create(Errors.codes.SHOULD_BE_INHERITED);
  }
}